/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll maxn = 10000;
ll dp[maxn];
ll v[maxn], val[maxn], c[maxn]; //c为在多重背包中没中物品的个数
ll n, m;                        //n 物品种类数 m 背包容量
//01背包
void zeroonepark(ll vi, ll vali)
{
    for (ll j = m; j >= vi; j--)
    {
        dp[j] = max(dp[j], dp[j - vi] + vali);
    }
    return;
}
//完全背包
void completepark(ll vi, ll vali)
{
    for (ll j = vi; j <= m; j++)
    {
        dp[j] = max(dp[j], dp[j - vi] + vali);
    }
    return;
}
//多重背包
void Multiplepark(ll vi, ll vali, ll ci)
{
    if (ci * vi > m)
    {                           //相当于无限个
        completepark(vi, vali); //完全背包
    }
    else
    {
        //二进制分组进行01背包
        ll k = 1;
        //倍增
        while (k < ci)
        {
            zeroonepark(k * vi, k * vali);
            ci -= k;
            k <<= 1;
        }
        if (ci > 0)
        {
            zeroonepark(ci * vi, ci * vali);
        }
    }
    return;
}

int main()
{
    IOS;
    ll t;
    cin >> t;
    while (t--)
    {
        cin >> m >> n;
        for (ll i = 1; i <= n; i++)
        {
            cin >> v[i] >> val[i] >> c[i];
            dp[i] = 0;
        }
        for (ll i = 1; i <= n; i++)
        {
            Multiplepark(v[i], val[i], c[i]);
        }
        cout << dp[m] << endl;
    }
    getchar();
    getchar();
    return 0;
}
